package com.example.company_project_fangyu.demo.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.company_project_fangyu.demo.common.BasicData;
import com.example.company_project_fangyu.demo.common.LoginSet;
import com.example.company_project_fangyu.demo.utils.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class StrangerLibraryService {

    @Autowired
    HttpClientUtils httpClientUtils;
    @Autowired
    LoginSet loginSet;

    /**
     * 添加陌生人底库
     */
    public JSON addStrangerLibrary(String name) {
        JSONObject params = new JSONObject();
        params.put("name",name);
        String url = BasicData.BASIC_URL + "/api/open/stranger/library";
        String res = null;
        try {
            res = httpClientUtils.doPostJson(url, params.toString(), loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }

    /**
     * 获取陌生人底库列表
     */
    public JSON getStrangerLibraryList() {
        String url = BasicData.BASIC_URL + "/api/open/stranger/library/list";
        String res = null;
        try {
            res = httpClientUtils.doGet(url, null,loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }

    /**
     * 修改陌生人底库
     */
    public JSON updateStrangerLibrary(String id,String name) {
        JSONObject params = new JSONObject();
        params.put("id",id);
        params.put("name",name);
        String url = BasicData.BASIC_URL + "/api/open/stranger/library";
        String res = null;
        try {
            res = httpClientUtils.doPutJson(url, params.toString(), loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }

    /**
     * 删除陌生人底库
     */
    public JSON deleteStrangerLibrary(String libraryId) {
        JSONObject params = new JSONObject();
        String url = BasicData.BASIC_URL + "/api/open/stranger/library/" + libraryId;
        String res = null;
        try {
            res = httpClientUtils.doDelete(url, loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }

    /**
     * 分页查询陌生人
     */
    public JSON getStrangerList(Integer page,Integer pageSize,String libraryId) {
        JSONObject params = new JSONObject();
        params.put("pageIndex",page.toString());
        params.put("pageSize",pageSize.toString());
        params.put("libraryId",libraryId);
        String url = BasicData.BASIC_URL + "/api/open/stranger/query";
        String res = null;
        try {
            res = httpClientUtils.doPostJson(url, params.toString(), loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }

    /**
     * 陌生人轨迹分页查询
     */
    public JSON getStrangerTrackList(Integer page,Integer pageSize,String libraryId,String strangerNo) {
        JSONObject params = new JSONObject();
        params.put("pageIndex",page.toString());
        params.put("pageSize",pageSize.toString());
        params.put("libraryId",libraryId);
        params.put("strangerNo",strangerNo);
        String url = BasicData.BASIC_URL + "/api/open/stranger/tracks/query";
        String res = null;
        try {
            res = httpClientUtils.doPostJson(url, params.toString(), loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }

    /**
     * 陌生人删除
     */
    public JSON deleteStranger(String libraryId,String strangerNo) {
        JSONObject params = new JSONObject();
        String url = BasicData.BASIC_URL + "/api/open/stranger/" + libraryId + "/" + strangerNo;
        String res = null;
        try {
            res = httpClientUtils.doDelete(url, loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }

}
